package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.model.net.bean.MemberListBean;
import com.cljz.makepassenger.present.activity.MemberListP;
import com.cljz.makepassenger.ui.adapter.MemberListAdapter;
import com.jakewharton.rxbinding.view.RxView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/5/5.
 */

public class MemberListActivity extends BaseActivity<MemberListP> implements XRecyclerView.LoadingListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.xrv_member)
    XRecyclerView xrvMember;
    List<MemberListBean.DataBean> memberList;
    private int page = 1;
    private boolean isLoadMore = false;
    private int type;
    private String id;
    private MemberListAdapter memberListAdapter;
    private boolean isRefresh;

    @Override
    public void initData(Bundle savedInstanceState) {
        showLoadingDialog();
        type = getIntent().getExtras().getInt("type");
        id = getIntent().getExtras().getString("id");
        tvTitleName.setText("查看参与的人");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrvMember.setLayoutManager(linearLayoutManager);
        memberListAdapter = new MemberListAdapter(this, memberList);
        xrvMember.setAdapter(memberListAdapter);
        xrvMember.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
        xrvMember.setLoadingMoreEnabled(true);
        xrvMember.setPullRefreshEnabled(true);
    }

    @Override
    protected void initListener() {
        super.initListener();
        xrvMember.setLoadingListener(this);
        xrvMember.refresh();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop(MemberListActivity.this);
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_member_list;
    }

    @Override
    public MemberListP newP() {
        return new MemberListP();
    }

    public void memberListSucceed(List<MemberListBean.DataBean> dataBeanList) {
        dismissLoadingDialog();
        if (isLoadMore) {
            xrvMember.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                xrvMember.setNoMore(true);
            } else {
                memberListAdapter.addData(dataBeanList);
            }
            isLoadMore = false;
        } else if (isRefresh){
            xrvMember.setNoMore(false);
            memberList = dataBeanList;
            memberListAdapter.setData(memberList);
            xrvMember.refreshComplete();
            isRefresh = false;
        }
    }

    public void memberListFail(String error) {
        if (isLoadMore) {
            xrvMember.setNoMore(true);
            isLoadMore = false;
            xrvMember.loadMoreComplete();
        }else {
            xrvMember.refreshComplete();
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        isRefresh = true;
        getP().memberList(type,id,page);
    }

    @Override
    public void onLoadMore() {
        page++;
        isLoadMore = true;
        getP().memberList(type, id, page);
    }
}
